package executor;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author tongchen
 * @create 2023-02-11 19:55
 */
public class TestCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int num=0;
                for (int i=1;i<=100;++i){
                    num+=i;
                }
                return num;
            }
        };

        //使用futureTask获取其返回值
        FutureTask<Integer> integerFutureTask = new FutureTask<>(callable);
        Thread thread = new Thread(integerFutureTask);
        thread.start();
        System.out.println(integerFutureTask.get());
    }
}
